package model

import (
	"fmt"
	"gin-admin/global"

	"github.com/bwmarrin/snowflake"
	"gorm.io/gorm"
)

type BaseModel struct {
	ID          string `gorm:"primaryKey;comment:主键id"`
	CreatedAt   int64  `gorm:"autoCreateTime:milli;comment:创建时间"`
	UpdatedAt   int64  `gorm:"autoUpdateTime:milli;comment:更新时间"`
	DeletedAt   int64  `gorm:"index;milli;comment:删除时间"`
	IsDelete    bool   `gorm:"default:false;comment:是否删除"`
	Status      int    `gorm:"default:1;comment:状态"`
	Description string `gorm:"size:1024;comment:描述"`
	Sort        int    `gorm:"comment:排序字段"`
}

func (bm *BaseModel) BeforeCreate(tx *gorm.DB) (err error) {
	if global.GA_LOGGER == nil {
		fmt.Println("GA_LOGGER 未初始化")
		return fmt.Errorf("GA_LOGGER 未初始化")
	}
	node, err := snowflake.NewNode(1)
	if err != nil {
		global.GA_LOGGER.Sugar().Fatalf("主键id创建失败: %v", err)
		return err
	}
	if bm.ID == "" {
		bm.ID = node.Generate().String()
	}
	return nil
}

func (bm *BaseModel) GetID() string {
	return bm.ID
}
